﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace VisionCloud.Domain.Data
{
    public class DataItem
    {
        public DataItem()
        { }

        public DataItem(string text, string value)
        {
            Text = text;
            Value = value;
        }

        /// <summary>
        /// 选项的文本
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 选项的值
        /// </summary>
        public string Value { get; set; }
    }

    public class SelectItem : DataItem
    {
        public SelectItem(string text, string value) : base(text, value)
        {
        }

        /// <summary>
        /// 是否已选中
        /// </summary>
        public bool Selected { get; set; }

        public static IEnumerable<SelectItem> ToItems(string[] items, bool addDefault)
        {
            return ToItems(items.Select(w => new SelectItem(w, w)).ToList(), addDefault);
        }

        public static IEnumerable<SelectItem> ToItems(SelectItem[] items, bool addDefault)
        {
            return ToItems(new List<SelectItem>(items), addDefault);
        }

        public static IEnumerable<SelectItem> ToItems<T>(bool addDefault)
        {
            var items =
                (from object e in Enum.GetValues(typeof(T))
                 select new SelectItem(e.ToString(), ((int)e).ToString())).ToList();
            return ToItems(items, addDefault);
        }

        private static IEnumerable<SelectItem> ToItems(IList<SelectItem> items, bool addDefault)
        {
            if (addDefault)
            {
                items.Insert(0, new SelectItem("-- 请选择 --", ""));
            }
            return items;
        }
    }
}